package com.longrise.common.utils;

import java.lang.reflect.ParameterizedType;

/**
 * Created by callxkj on 2017/2/13.
 * 类上面的泛型转化成对象
 */

public class GenericUtil {

    public static <T> T getT(Object o, int i) {
        try {
            if (o.getClass().getGenericSuperclass() instanceof ParameterizedType) {
                return ((Class<T>) ((ParameterizedType) o.getClass()
                        .getGenericSuperclass())
                        .getActualTypeArguments()[i]).newInstance();
            } else {
                return null;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

}
